from rest_framework.serializers import ModelSerializer
from rest_framework import serializers
from homes import models


class Category(ModelSerializer):
    children = serializers.SerializerMethodField()

    def get_children(self, obj):
        child = models.Category.objects.filter(parent=obj.id)
        if child:
            return Category(child, many=True).data
        return []


    class Meta:
        model = models.Category
        fields = [
            'id',
            'name',
            'level',
            'parent',
            'children'

        ]



class BannerSerializer(ModelSerializer):
    class Meta:
        model = models.Banner
        fields = [
            'id',
            'title',
            'imgUrl',
            'link',
        ]
